package com.pignest.framework.util;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;

import org.apache.commons.lang.StringUtils;

public class Utils {

	public static int getInteger(String str, int def) {
		try {
			return Integer.parseInt(str);
		} catch (NumberFormatException e) {
		}
		return def;
	}
	
	/** 系统中所有金钱相关存储以分为单位, 转换为元为单位显示 */
	public static String toPrice(long n) {
		int num = (int)n;
		if (num % 100 == 0)
			return String.valueOf(num / 100);
		if (num % 10 == 0) {
			return (num / 100) + "." + (Math.abs(num / 10) % 10);
		}
		return String.format("%d.%02d", num / 100, num % 100);
	}
	
	public static boolean checkPrice(String price) {
		return price != null && price.matches("\\d+(\\.\\d{0,2})?");
	}
	
	/** 输入的价格(单位为元)转为数据库存储的数值(单位为分) */
	public static int toPrice(String price) {
		return toPrice(price, 0);
	}
	public static int toPrice(String price, int def) {
		try {
			if (price == null)
				return def;
			float v = Float.parseFloat(price.trim());
			BigDecimal bd = new BigDecimal(v * 100).setScale(0, BigDecimal.ROUND_FLOOR);
			return bd.intValue();
		} catch (NumberFormatException e) {
		}
		return def;
	}
	
	public static String toPrecision(double num, int precision) {
		BigDecimal bd = new BigDecimal(num).setScale(precision, BigDecimal.ROUND_HALF_UP);
		return bd.toString();
	}
	
	/**
	 * 用于页面显示
	 * */
	public static String showString(String str) {
		return str == null ? "" : str;
	}
	
	public static String toString(Throwable e) {
		if (e == null)
			return null;
		StringBuilder sb = new StringBuilder();
		sb.append(e.getClass().getName()).append(":");
		StackTraceElement trace = e.getStackTrace()[0];
		sb.append(trace.getLineNumber()).append("@").append(trace.getFileName());
		sb.append("#").append(trace.getMethodName());
		return sb.toString();
	}
	
	/** 
	 * 获取扩展名
	 * */
	public static String getExtention(String fileName) {
		if (fileName == null)
			return null;
		int i = fileName.lastIndexOf('.');
		if (i == -1)
			return null;
		String ext = fileName.substring(i + 1);
		if (ext.indexOf('/') != -1 || ext.indexOf('\\') != -1)
			return null;
		return ext.toLowerCase();
	}
	
	/**
	 * 编码转换
	 * */
	public static String e(String s) {
		try {
			if (s != null)
				return new String(s.getBytes("ISO-8859-1"), "UTF-8");
		} catch (UnsupportedEncodingException e1) {
		}
		return s;
	}

	public static void showBinary(String str) {
		if (str == null)
			return;
		char[] bs = str.toCharArray();
		for (int i = 0; i < bs.length; i++) {
			System.out.print(Integer.toString(bs[i] & 0xffffffff, 16));
			System.out.print(' ');
		}
	}
	
	
	public static boolean equals(String str1, String str2) {
		if (str1 == str2)
			return true;
		return StringUtils.isEmpty(str1) ? StringUtils.isEmpty(str2) : str1.equals(str2);
	}
	
	public static void main(String[] args) {
		//System.out.println(MoneyUtil.toString(33333));
		System.out.println(Utils.toPrice(19850));
		System.out.println(Utils.checkPrice("198.5"));
		System.out.println(Utils.toPrice("198"));
		System.out.println(Utils.toPrice("1."));
		System.out.println(Utils.toPrice("198.5"));
		System.out.println(Utils.toPrice("198.51"));
		System.out.println(Utils.toPrice("198.5123"));
		System.out.println(Utils.toPrice("198.45123"));
	}
	
}
